package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.mapper.AdminMapper;
import com.dyb.recuitment.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 管理员用户表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public Admin getByAdmin(Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>(admin);
        List<Admin> result = adminMapper.selectList(queryWrapper);
        if (result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public int updateAdmin(Admin admin) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        admin.setUpdateTime(localDateTime);
        return adminMapper.updateById(admin);
    }

    @Override
    public int updatePwd(Admin admin) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        admin.setPassword(password).setUpdateTime(localDateTime);
        return adminMapper.updateById(admin);
    }

    @Override
    public int deleteAdmin(Admin admin) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        admin.setStatus(0).setUpdateTime(localDateTime);
        return adminMapper.updateById(admin);
    }

    @Override
    public List<Admin> selectByUser(Admin admin) {
        admin.setStatus(1);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>(admin);
        return adminMapper.selectList(queryWrapper);
    }

    @Override
    public List<Admin> selectAllAdmin() {
        return adminMapper.selectList(new QueryWrapper<>());
    }
}
